package com.ktjy.auth.handler;

import com.alibaba.fastjson.JSON;
import com.ktjy.base.R;
import com.ktjy.util.WebUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

/**
 * 权限不足处理器
 *
 * @author LILIBO
 * @since 2023-02-27
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {

    /**
     * 权限不足
     */
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) {
        // 将结果写回浏览器
        WebUtil.writerString(response, JSON.toJSONString(R.failure(HttpStatus.FORBIDDEN.value(), "权限不足")));
    }

}
